package com.wecan.micro.feign;

import com.alibaba.fastjson.JSONObject;
import com.wecan.base.exception.app.FeignException;
import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Type;

/**
 * @Author liuzhihao
 * @Date 2022.01.16
 * @Version ${version}
 * @Description ${description}
 */
@Slf4j
public class FeignErrorDecoder implements ErrorDecoder {

    @Override
    public Exception decode(String s, Response response) {
        try {
            String message = Util.toString(response.body().asReader());
            JSONObject jsonObject = JSONObject.parseObject(message);
            String msg = jsonObject.getString("msg");
            log.info("[APP] |- 服务间调用失败 [{}]", msg);
            return new FeignException();
        } catch (Exception ex) {
            return new FeignException();
        }
    }
}
